package com.owc.common.custom.impl;

import com.owc.common.custom.module.EncryptedJsonSerializer;
import com.owc.common.custom.module.EncryptedSerializerModifier;
import lombok.NoArgsConstructor;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.Module;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
 * @Author xiaom
 * @Date 2019/12/3 10:07
 * @Version 1.0.0
 * @Description <>
 **/
public class EncryptModule extends Module {

    public final static String ARTIFACT_ID = "jackson-owc-encryption";
    private final static Version version =  new Version(1,0,0,null);

    public EncryptModule() {
    }

    @Override
    public String getModuleName() {
        return ARTIFACT_ID;
    }

    @Override
    public Version version() {
        return version;
    }

    @Override
    public void setupModule(SetupContext setupContext) {
        setupContext.addBeanSerializerModifier(new EncryptedSerializerModifier());
//        setupContext.addBeanDeserializerModifier(new EncryptedJsonSerializer());
    }

    /**
     * 创建一个{@link ObjectMapper}对象，支持{@link com.owc.common.custom.annotation.Encrypt}注解。
     */
    public static ObjectMapper createMapper(){
        ObjectMapper objectMapper  = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure( SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.registerModule(new EncryptModule());
        return objectMapper;
    }
}
